<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="租户名称" prop="tenantName">
        <el-input
          v-model="queryParams.tenantName"
          placeholder="请输入租户名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户状态" prop="tenantStatus">
        <el-select
          v-model="queryParams.tenantStatus"
          placeholder="租户状态"
          clearable
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:tenant:add']"
          >新增</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:tenant:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:tenant:remove']"
          >删除</el-button
        >
      </el-col> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="dataList"
      @selection-change="handleSelectionChange"
    >      
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column prop="id" align="center" v-if="false"/>
      <el-table-column label="租户编号" prop="tenantNo" align="center"/>
      <el-table-column label="租户名称" prop="tenantName" align="center"/>
      <el-table-column label="租户官网" prop="tenantWebsite" align="center" />
      <el-table-column label="租户logo" prop="tenantLogo" align="center" >
        <template slot-scope="scope">
            <div align="center">
                <img v-if="scope.row.tenantLogo" :src="imgPath + scope.row.tenantLogo" class="avatar1">
            </div>
            <!-- <i v-else class="el-icon-picture"></i> -->
        </template>
      </el-table-column>
      <el-table-column label="租户类型" prop="tenantType" align="center" />
      <el-table-column label="租户状态" align="center" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.tenantStatus"
            active-value="1"
            inactive-value="0"
            @change="handleStatusChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="描述" prop="descContent" align="center"/>
      <el-table-column label="创建时间" align="center" prop="createTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="modifyTime" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.modifyTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button
                  size="mini"
                  circle type="success"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['system:tenant:update']"
          />
          <el-button
                  size="mini"
                  circle type="danger"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['system:tenant:delete']"
          />
        </template>
      </el-table-column>
    </el-table>
    
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog :title="title" :visible.sync="open" width="800px">
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
            <el-row :gutter="6">
                <el-col :span="12">
                    <el-form-item label="租户编号" prop="tenantNo">
                        <el-input v-model="form.tenantNo" placeholder="请输入租户编号" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="租户名称" prop="tenantName">
                    <el-input v-model="form.tenantName" placeholder="请输入租户名称" />
                    </el-form-item>                    
                </el-col>
            </el-row>
            <el-row :gutter="6">
                <el-col :span="12">
                    <el-form-item label="官网地址" prop="tenantWebsite">
                        <el-input v-model="form.tenantWebsite" placeholder="请输入官网地址" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="租户类型" prop="tenantType">
                        <el-select v-model="form.tenantType" placeholder="请选择租户类型">
                            <el-option
                            v-for="item in tenantTypeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>            

            <el-row :gutter="6">
                <el-col :span="12">
                    <el-form-item label="租户状态" prop="tenantStatus">
                        <el-switch
                            v-model="form.tenantStatus"
                            active-value="1"
                            inactive-value="0"
                        />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="租户logo" prop="tenantLogo">
                        <el-upload
                            class="avatar-uploader"
                            :action="activeUrl"
                            :show-file-list="false"
                            :on-success="handleAvatarSuccess"
                            :before-upload="beforeAvatarUpload">
                            <img v-if="form.tenantLogo" :src="imgPath + form.tenantLogo" class="avatar">
                            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                        </el-upload>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="6">
                <el-col :span="12">
                    <el-form-item label="经度">
                        <!-- <el-input v-model="form.lng" /> -->
                        {{form.lng}}
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="纬度">
                        {{form.lat}}
                        <!-- <el-input v-model="form.lat" /> -->
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="地图">
                <div style="mapClass">
                    <!-- //:lng="form.lng" :lat="form.lat" -->
                    <Mapview v-if="reFresh" :parentLng="form.lng" :parentLat="form.lat" @send-location="receiveLocation"></Mapview>
                </div>
            </el-form-item>
            <el-form-item label="描述" prop="descContent">
                <el-input v-model="form.descContent" type="textarea" placeholder="请输入描述" />
            </el-form-item>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { addTenant,findPageList,updateTenant,deleteTenant } from "@/api/system/tenant";
import Mapview from "@/components/map/MapContainer";

export default {
  name: "tenant",
  components: { Mapview },
  data() {
    return {
      //图片路径
      imgPath: process.env.VUE_APP_BASE_API + '/school/file/',
      //上传的路径
      activeUrl:'',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 角色表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [
        {
          dictLabel: "启用",
          dictValue: "0",
        },
        {
          dictLabel: "停用",
          dictValue: "1",
        },
      ],
      tenantTypeList:[
        {
            "label":"type",
            "value":"1"
        }
      ],
      //查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      rules:{},
      reFresh:true,
    };
  },
  watch:{
    form(){
        this.reFresh= false
        this.$nextTick(()=>{
            this.reFresh = true
        })
    }
  },
  computed: {
    headers() {
      return {
        'Authorization': getToken()
      }
    }
  },
  created() {
    this.getList();
  },

  methods: {
    receiveLocation(val){
        console.log(val)
        this.form.lng = val.lng;
        this.form.lat = val.lat;
        this.$forceUpdate()
        // console.log(this.form)
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        tenantNo: undefined,
        tenantName: undefined,
        tenantWebsite: undefined,
        tenantType: undefined,
        tenantStatus: undefined,
        tenantLogo: undefined,
        descContent: undefined
      };
      this.resetForm("form");
    },
    // 查询
    getList() {
        this.loading = true;
        findPageList(this.queryParams).then(resp =>{
            this.dataList = resp.data.list
            this.total = resp.data.total
            this.loading = false
        })
    },
    // 新增
    handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增租户";
        this.activeUrl = process.env.VUE_APP_BASE_API +'/test/uploadImage';
    },
    //提交
    submitForm:function(){
        this.$refs["form"].validate(valid =>{
            if (valid){
                if (this.form.id !== undefined){
                    //更新
                    updateTenant(this.form).then(resp => {
                        if (resp.code === 200) {
                            this.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        } else {
                            this.msgError(resp.message);
                        }                        
                    })
                }else{
                    //新增
                    addTenant(this.form).then(resp => {
                        if (resp.code === 200) {
                            this.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        } else {
                            this.msgError(resp.message);
                        }
                    })
                }
            }
        })
    },
    // 取消按钮
    cancel() {
      this.open = false;
    //   this.reset();
    },
    // 更新
    handleUpdate(row) {
        this.form = row
        this.title = "修改租户信息"
        this.open = true;
        this.activeUrl = process.env.VUE_APP_BASE_API +'/test/uploadImage';
    },
    // 删除
    handleDelete(row) {
        this.$confirm('确认要删除【' + row.tenantName + '】租户吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function(){
            return deleteTenant(row.id);
        }).then(() => {
            this.msgSuccess("删除成功");
            this.getList()
        }).catch(function(){

        })
    },
    handleStatusChange(row){
        let text = row.tenantStatus === "0" ?  "停用":"启用" ;
        this.$confirm('确认要【' + text + '】-【' + row.tenantName + '】租户吗?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
            let obj = {
                "id":row.id,
                "tenantStatus":row.tenantStatus
            }
          return updateTenant(obj);
        }).then(() => {
          this.msgSuccess(text + "成功");
          this.getList()
        }).catch(function() {
          row.tenantStatus = row.tenantStatus === "0" ? "1" : "0";
        });
    },

    //上传logo成功回调
    handleAvatarSuccess(res, file) {
        // this.form.tenantLogo = "http://127.0.0.1:8088" + '/school/file/' + res.result; 
        this.form.tenantLogo = res.result; 
    },
    //上传logo前校验
    beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
            this.$message.error('上传logo图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
            this.$message.error('上传logo图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
    },

  },
};
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 78px;
    height: 78px;
    line-height: 78px;
    text-align: center;
  }
  .avatar {
    width: 78px;
    height: 78px;
    display: block;
  }
  .avatar1 {
    width: 78px;
    height: 78px;
    display: block;
  }

  .mapClass{  
    height:330px;
    margin-bottom:8px
  }
</style>